﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hired
{
    public partial class viewjsprofile : System.Web.UI.Page
    {
        private JobseekerAccount profile = new JobseekerAccount();

        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request.QueryString["username"] != null)
            {
                string profileUsername = Request.QueryString["username"];
                profile = DBJobseekerAccount.retrieveJobseekerAccountbyUsername(profileUsername, true);

                if (profile == null)
                {
                    Response.Redirect("home.aspx");
                }

                lblUsername.Text = profile.Username;
                lblBuddyUsername.Text = profile.Username;
                lblRecipient.Text = profile.Username;
                eds.WhereParameters[0].DefaultValue = profile.JAccountID;
            }
            else
            {
                Response.Redirect("home.aspx");
            }

            if (!DBBuddyRoster.checkBuddy(User.Identity.Name, profile.JAccountID))
            {
                btnAddBuddy.Visible = false;
            }

            //Wall Thread
            List<Thread> wallThread = DBThread.RetrieveThreadsByUserID(profile.JAccountID);

            if (wallThread.Count != 0)
            {
                foreach (Thread t in wallThread)
                {
                    #region Thread
                    UCComments tc = (UCComments)LoadControl("UCComments.ascx");
                    pnlContainer.Controls.Add(tc);

                    // accessing ThreadComment.ascx controls
                    Image threadImgProfilePic = (Image)tc.FindControl("imgProfilePic");
                    Label threadLblCommentContent = (Label)tc.FindControl("lblCommentContent");
                    LinkButton threadBtnUsername = (LinkButton)tc.FindControl("btnUsername");

                    // assigning values to them
                    if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                    {
                        JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                        threadBtnUsername.Text = jsPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                    }
                    else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                    {
                        CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                        threadImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                        threadBtnUsername.Text = coPoster.Username;
                        threadBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                    }
                    threadLblCommentContent.Text = t.Contents;

                    #endregion

                    #region Comments
                    if (t.Comments.Count != 0)
                    {
                        foreach (Comment c in t.Comments)
                        {
                            UCComments comment = (UCComments)Page.LoadControl("UCComments.ascx");
                            pnlContainer.Controls.Add(comment);

                            // accessing ThreadUC.ascx controls
                            Panel commentPnlComment = (Panel)comment.FindControl("pnlComment");
                            Image commentImgProfilePic = (Image)comment.FindControl("imgProfilePic");
                            Label commentLblCommentContent = (Label)comment.FindControl("lblCommentContent");
                            LinkButton commentBtnUsername = (LinkButton)comment.FindControl("btnUsername");

                            // assigning values to them
                            commentPnlComment.CssClass = "pnlComment";
                            if (DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true) != null)
                            {
                                JobseekerAccount jsPoster = DBJobseekerAccount.retrieveJobseekerAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = jsPoster.Jobseeker.ImageAddress;
                                commentBtnUsername.Text = jsPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + jsPoster.Username;
                            }
                            else if (DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true) != null)
                            {
                                CompanyAccount coPoster = DBCompanyAccount.RetrieveCompanyAccount(t.PosterID, true);
                                commentImgProfilePic.ImageUrl = coPoster.Company.ImageAddress;
                                commentBtnUsername.Text = coPoster.Username;
                                commentBtnUsername.PostBackUrl = "viewprofile.aspx?username=" + coPoster.Username;
                            }
                            commentLblCommentContent.Text = c.Contents;
                        }
                    }
                    #endregion

                    UCPostComment pc = (UCPostComment)Page.LoadControl("UCPostComment.ascx");
                    ((Label)pc.FindControl("lblThreadID")).Text = t.ThreadID;
                    pnlContainer.Controls.Add(pc);

                    Button btnPost = (Button)pc.FindControl("btnPost");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    
        
        

        protected void btnWall_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlWall.Visible = true;
        }

        protected void btnInformation_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlPersonalInformation.Visible = true;

            profile = DBJobseekerAccount.retrieveJobseekerAccount(profile.JAccountID, true);
            #region Initializing values for display
            lblFirstNameValue.Text = profile.Jobseeker.FirstName;
            lblLastNameValue.Text = profile.Jobseeker.LastName;
            switch (profile.Jobseeker.Gender)
            {
                case "M":
                    lblGenderValue.Text = "Male";
                    break;
                case "F":
                    lblGenderValue.Text = "Female";
                    break;
            }
            lblDateOfBirthValue.Text = profile.Jobseeker.DateOfBirth.Value.ToLongDateString();
            lblNationalityValue.Text = profile.Jobseeker.Nationality;
            lblCountryOfResidenceValue.Text = profile.Jobseeker.CountryOfResidence;
            if (profile.Jobseeker.WebsiteURL != null && profile.Jobseeker.WebsiteURL != "")
            {
                lblWebsiteValue.Text = profile.Jobseeker.WebsiteURL;
            }
            else
            {
                lblWebsite.Visible = false;
                lblWebsiteValue.Visible = false;
            }

            lblEmailValue.Text = profile.Email;
            if (profile.Jobseeker.HomeNumber != null && profile.Jobseeker.HomeNumber != "")
            {
                lblHomeNumberValue.Text = profile.Jobseeker.HomeNumber + "<br /><br />";
            }
            else
            {
                lblHomeNumber.Visible = false;
                lblHomeNumberValue.Visible = false;
            }
            if (profile.Jobseeker.MobileNumber != null && profile.Jobseeker.MobileNumber != "")
            {
                lblMobileNumberValue.Text = profile.Jobseeker.MobileNumber;
            }
            else
            {
                lblMobileNumber.Visible = false;
                lblMobileNumberValue.Visible = false;
            }

            lblEducationLevelValue.Text = profile.Jobseeker.EducationLevel;
            if (profile.Jobseeker.PreviousEducationalInstitute != null && profile.Jobseeker.PreviousEducationalInstitute != "")
            {
                lblPreviousEducationalInstituteValue.Text = profile.Jobseeker.PreviousEducationalInstitute + "<br /><br />";
            }
            else
            {
                lblPreviousEducationalInstitute.Visible = false;
                lblPreviousEducationalInstituteValue.Visible = false;
            }
            if (profile.Jobseeker.Specialization != null && profile.Jobseeker.Specialization != "")
            {
                lblSpecializationValue.Text = profile.Jobseeker.Specialization + "<br /><br />";
            }
            else
            {
                lblSpecialization.Visible = false;
                lblSpecializationValue.Visible = false;
            }
            if (profile.Jobseeker.PreviousEmployment != null && profile.Jobseeker.PreviousEmployment != "")
            {
                lblPreviousEmploymentValue.Text = profile.Jobseeker.PreviousEmployment + "<br /><br />";
            }
            else
            {
                lblPreviousEmployment.Visible = false;
                lblPreviousEmploymentValue.Visible = false;
            }
            if (profile.Jobseeker.CurrentEmployment != null && profile.Jobseeker.PreviousEmployment != "")
            {
                lblCurrentEmploymentValue.Text = profile.Jobseeker.CurrentEmployment;
            }
            else
            {
                lblCurrentEmployment.Visible = false;
                lblCurrentEmploymentValue.Visible = false;
            }
            #endregion
        }

        protected void btnBuddyList_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlBuddyList.Visible = true;

            lvBuddyList.DataSource = DBBuddyRoster.viewBuddyImageNameList(profile.JAccountID);
            lvBuddyList.DataBind();
        }

        protected void btnImages_Click(object sender, ImageClickEventArgs e)
        {
            hidePanels();
            pnlImages.Visible = true;
        }

        protected void btnMessage_Click(object sender, ImageClickEventArgs e)
        {
            
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            Thread t = new Thread();
            t.PosterID = User.Identity.Name;
            t.WallID = profile.JAccountID;
            t.Contents = txtComment.Text;
            t.Timestamp = System.DateTime.Now;

            DBThread.AddThread(t);
        }

        protected void hidePanels()
        {
            pnlWall.Visible = false;
            pnlPersonalInformation.Visible = false;
            pnlBuddyList.Visible = false;
            pnlImages.Visible = false;
        }

        protected void btnAddBuddy_Click(object sender, ImageClickEventArgs e)
        {
            BuddyRequestRoster brr = new BuddyRequestRoster();
            brr.SenderID = User.Identity.Name;
            brr.RecipientID = profile.JAccountID;

            if (DBBuddyRequestRoster.addBuddyRequest(brr))
            {
                lblConfirmBuddyUsername.Text = "A buddy request has been sent to " + profile.Username + ".";
            }
            else
            {
                lblConfirmBuddyUsername.Text = "Failed to add " + profile.Username + " as buddy. Have you already added him?";
            }

            ModalPopupBuddyConfirm.Show();
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            Message m = new Message();
            m.RecipientID = profile.JAccountID;
            m.SenderID = User.Identity.Name;
            m.Title = txtSubject.Text;
            m.Contents = txtContent.Text;
            m.Timestamp = DateTime.Now;

            DBMessage.AddMessage(m);
        }
    }
}